const { update, remove, add, getCount, query, getOne } = require('../../utils/dbTools');
const tableName = 'finance';
const patientTableName = 'patient';

/**
 * 根据患者id查询医生信息
 * @params {Number} patientid: 患者id
*/

const findDoctorIdByPatientId = async (patientid) => {
    const res = await getOne(patientTableName, { id: patientid });
    if (res) {
        return res.doctorid;
    }
    return false;
}

/**
 * 获取数据总条数
 * 
*/

const financeTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const result = await getCount(tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}

/**
 * 获取收入支出列表
 * 
*/
const financeList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query;
    if (!page || !size) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    let sql = `select f.id,f.money,f.type,f.pay_way,f.trade_time,f.create_time,p.name as patient,p.phone as patientPhone,`;
    sql += ` s.name as doctor, s.phone as doctorPhone from ${tableName} f inner join patient p on f.patientid=p.id `;
    sql += ` inner join staff s on f.doctorid=s.id order by f.id desc limit ${(page - 1) * size},${size}`;
    const result = await query(sql);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}

/**
 * 新增收入支出信息
 * 
*/

const financeAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { patientid, money, pay_way, trade_time, type } = req.body;
    if (!patientid || !money || !pay_way || !trade_time || !type) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // 将日期字符串转换为时间戳
    if(trade_time) trade_time=new Date(trade_time).getTime();
    const data = {
        patientid, money, pay_way, type, trade_time, create_time: Date.now()
    }
    // 根据患者id查询医生id
    if (patientid) data.doctorid = await findDoctorIdByPatientId(patientid);
    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
        return res.send(output);
    }
    res.send(output);
}

/**
 *  删除收入支出信息
 * 
*/

const financeRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }

    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }

    const result = await remove(id, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

/**
 * 更新收入支出信息: 仅支持更新状态
*/
const financeUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { id, patientid, money, pay_way, trade_time, type } = req.body;
    if (!id || !patientid || !money || !pay_way || !trade_time || !type) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // 将日期字符串转换为时间戳
    if(trade_time) trade_time=new Date(trade_time).getTime();
    const data = {
        patientid, money, pay_way, type, trade_time, create_time: Date.now()
    }
    // 根据患者id查询医生id
    if (patientid) data.doctorid = await findDoctorIdByPatientId(patientid);
    const result = await update(id, data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

/**
 * 统计销售业绩
 * 
*/

const financeStats=(req,res)=>{
    const output = {
        code: 200,
        msg: '操作成功',
        data:[
            [42000,50333,38992,49992,65568,75623,59882],[4400,2299,2988,2544,3300,4100,3300]
        ]
    }
    res.send(output);
}


module.exports = {
    financeAdd,
    financeRemove,
    financeUpdate,
    financeList,
    financeTotal,
    financeStats
}